package com.xin.springcloudquartz.service.impl;

import com.xin.springcloudquartz.dataobject.Event;
import com.xin.springcloudquartz.dto.PageResult;
import com.xin.springcloudquartz.enums.EventStatusEnum;
import com.xin.springcloudquartz.repository.EventRepository;
import com.xin.springcloudquartz.service.EventService;
import com.xin.springcloudquartz.utils.JsonUtil;
import com.xin.springcloudquartz.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
@Slf4j
public class EventServiceImpl implements EventService{
    @Autowired
    private EventRepository eventRepository;

    /**
     * 创建事件
     * @param event
     * @return
     */
    @Transactional
    public Event create(Event event) {
        event.setStatus(EventStatusEnum.START.getCode());
        Event save = eventRepository.save(event);
        if (ObjectUtils.isEmpty(save)){
            log.info("【创建事件】创建失败!");
               return null;
        }
        return save;
    }

    /**
     * 修改事件
     * @param event
     * @return
     */
    @Transactional
    public Event update(Event event) {
        Event one = findOne(event.getId());
        if (ObjectUtils.isEmpty(one)){
            log.info("【修改事件】该事件不存在! id={}",event.getId());
              return null;
        }
        BeanUtils.copyProperties(event,one);
        Event save = eventRepository.save(one);
        if (ObjectUtils.isEmpty(save)){
             log.info("【修改事件】更新失败!");
             return null;
        }
        return save;
    }

    /**
     * 删除事件
     * @param id
     * @return
     */
    @Transactional
    public boolean delete(String id) {
        Event one = findOne(id);
        if (ObjectUtils.isEmpty(one)){
              log.info("【删除事件】");
              return false;
        }
        eventRepository.delete(one);
        return true;
    }

    /**
     * 查询某个事件
     * @param id
     * @return
     */
    public Event findOne(String id) {
        Event one = eventRepository.findOne(id);
        return one;
    }

    /**
     * 查询某人的事件列表
     * @param openId
     * @param pageable
     * @return
     */
    public PageResult findAll(String openId, Pageable pageable) {
        Page<Event> page = eventRepository.findAllByOpenId(openId, pageable);
        if (page!=null){
              PageResult<Event> pageResult=new PageResult<>();
              pageResult.setPageSize(page.getSize());
              pageResult.setPageNo(page.getTotalPages());
              pageResult.setContent(page.getContent());
              pageResult.setTotal(page.getTotalElements());
              return pageResult;
        }
        return null;
    }

    @Override
    public boolean pushQuartz(String openId, String date, String message) {

        return false;
    }
}
